package com.zny.sescore.model;

import com.zny.sescore.exception.IMsgEnum;
import com.zny.sescore.exception.MessageEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 接口返回对象
 *
 * @author zhangnayi
 * @date 2021/03/26 19:48
 */
@Data
@ApiModel("全局Api返回结构")
@AllArgsConstructor
@NoArgsConstructor
@SuppressWarnings("unchecked")
public class ApiResult<T> {

    @ApiModelProperty("编码")
    private Integer code;

    @ApiModelProperty("消息")
    private String message;

    @ApiModelProperty("数据是否被加密")
    private boolean dataEncrypt = false;

    @ApiModelProperty("数据")
    private T data;

    ApiResult(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 请求成功
     *
     * @author zhangnayi
     * @date 2021/03/26 19:48
     */
    public static <T> ApiResult<T> success(T data) {
        return new ApiResult<>(MessageEnum.BIZ_SUCCESS.getCode(), MessageEnum.BIZ_SUCCESS.getDisplayName(), data);
    }

    /**
     * 请求失败
     *
     * @author zhangnayi
     * @date 2021/03/26 19:48
     */
    public static ApiResult<Object> failed() {
        return new ApiResult<>(MessageEnum.BIZ_ERROR.getCode(), MessageEnum.BIZ_ERROR.getDisplayName(), null);
    }

    /**
     * 请求失败
     *
     * @author zhangnayi
     * @date 2021/03/26 19:48
     */
    public static ApiResult<Object> failed(IMsgEnum msgEnum) {
        return new ApiResult<>(msgEnum.getCode(), msgEnum.getDisplayName(), null);
    }

    /**
     * 请求失败
     *
     * @author zhangnayi
     * @date 2021/03/26 19:48
     */
    public static ApiResult<Object> failed(String message) {
        return new ApiResult<>(MessageEnum.BIZ_ERROR.getCode(), message, null);
    }

    /**
     * 请求失败
     *
     * @author zhangnayi
     * @date 2021/03/26 19:48
     */
    public static ApiResult<Object> failed(Integer code, String message) {
        return new ApiResult<>(code, message, null);
    }
}